package com.cu.simpleerp.system.service;

import com.cu.simpleerp.common.ResultModel;
import com.cu.simpleerp.frame.SpringContextUtil;
import com.cu.simpleerp.frame.session.SessionManager;
import com.cu.simpleerp.frame.session.SessionManagerUtil;
import com.cu.simpleerp.system.dao.UserMapper;
import com.cu.simpleerp.system.model.User;
import com.cu.simpleerp.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 用户控制类
 *
 * @author Administrator
 * @date 2018-10-24 23:08
 */
@Service
public class UserService {

    private Logger log = LoggerFactory.getLogger(UserService.class);

    @Autowired
    private UserMapper userDao;

    public User queryByUserName(String userName) {
        return userDao.selectByUserName(userName);
    }

    public ResultModel checkLogin(String userName, String password, HttpServletResponse response, HttpServletRequest request) {
        User user = userDao.selectByUserName(userName);
        log.info("login query user:" + (user == null ? "null" : user.toString()));
        if (user == null) {
            return new ResultModel("error", "用户不存在");
        }
        if (password.equals(user.getUserPassward())) {
            SessionManagerUtil.addSession(response, request, user.getId().toString());
            return new ResultModel("success", "登录成功");
        } else {
            return new ResultModel("error", "密码错误");
        }
    }

    public User queryById(Integer userId) {
        return userDao.selectByPrimaryKey(userId);
    }
}
